import pandas as pd
import numpy as np

file = r'Data\Cleaned_EVUsage_Data.csv'
file2 = r'Data\Derivational_EVUsage_Data.csv'
# 读取数据
df = pd.read_csv(file)

# 从时间列(Start Date)中提取新的时间特征，如年份、月份、小时
df['year'] = pd.to_datetime(df['Start Date']).dt.year
df['month'] = pd.to_datetime(df['Start Date']).dt.month
df['hour'] = pd.to_datetime(df['Start Date']).dt.hour
df['weekday'] = pd.to_datetime(df['Start Date']).dt.weekday

# 衍生新的特征，如是否为工作日、是否为高峰时段等
df['is_weekday'] = df['weekday'].apply(lambda x: 1 if x < 5 else 0)
df['is_peak'] = df['hour'].apply(lambda x: 1 if x >= 7 and x <= 9 or x >= 17 and x <= 19 else 0)

# 将列Charging Time (hh:mm:ss)转换为'timedelta'类型
df['Charging Time (hh:mm:ss)'] = pd.to_timedelta(df['Charging Time (hh:mm:ss)'])

# 将列Total Duration (hh:mm:ss)转换为'timedelta'类型
df['Total Duration (hh:mm:ss)'] = pd.to_timedelta(df['Total Duration (hh:mm:ss)'])

# 衍生一列Charging Efficiency, 即充电效率, 是充电时长除以总时长
df['Charging Efficiency'] = df['Charging Time (hh:mm:ss)'].dt.total_seconds() / df['Total Duration (hh:mm:ss)'].dt.total_seconds()

# 衍生一列表示充电效率等级的列
df['Charging Efficiency Level'] = pd.cut(df['Charging Efficiency'], bins=[0, 0.5, 0.7, 1], labels=['Low', 'Medium', 'High'])

# 衍生一列表示不同时间段的用户
df['Time of Day'] = pd.cut(df['hour'], bins=[0, 6, 12, 18, 24], labels=['Early Morning', 'Morning', 'Afternoon', 'Evening'])

# 衍生一列表示不同季节段的用户
bins = [1, 3, 6, 9, 12] # 定义区间和标签
labels = ['Winter', 'Spring', 'Summer', 'Fall']
df['Season'] = pd.cut(df['month'], bins=bins, labels=labels, right=False, include_lowest=True) # 使用 pd.cut 进行分区，确保区间是左闭右开
df.loc[df['month'] == 12, 'Season'] = 'Winter' # 将12月单独处理归入冬季

# Energy (kWh)除以 Charging Time (hh:mm:ss)得到Average Energy Consumption 平均充电功率
df['Average Energy Consumption'] = df['Energy (kWh)'] / (df['Charging Time (hh:mm:ss)'].dt.total_seconds() / 3600)

# Fee 除以 Energy (kWh)得到Average Cost per kWh 电价
df['Average Cost per kWh'] = df['Fee'] / df['Energy (kWh)']

# 将处理后的数据保存到新的CSV文件中
df.to_csv(file2, index=False)